/*
 * @Description: 全局Loading
 * @Author: 0001100986/LiuShuai
 * @Date: 2021-10-03 10:49:47
 * @LastEditTime: 2021-10-03 10:57:51
 * @LastEditors: 0001100986/LiuShuai
 */
import { Loading } from "element-ui";
import _ from 'lodash';

let loading = null;
let needRequestCount = 0;


// 开启loading状态
const startLoading = (headers = {}) => {
  loading = Loading.service({
    lock: true,
    text: headers.text || "加载中……",
    background: "rgba(0, 0, 0, 0.7)",
    target: headers.target || "body"
  });
};

// 关闭loading状态
const endLoading = _.debounce(() => {
  loading.close();
  loading = null;
}, 300);


export const showScreenLoading = (headers) => {
  if (needRequestCount === 0 && !loading) {
    startLoading(headers);
  }
  needRequestCount++;
};

export const hideScreenLoading = () => {
  if (needRequestCount <= 0) return;
  needRequestCount--;
  needRequestCount = Math.max(needRequestCount, 0);
  if (needRequestCount === 0) {
    endLoading();
  }
};
export default { showScreenLoading, hideScreenLoading };